"""
读取配置文件，选择运行模式
"""

from re_common.baselibrary.utils.basedir import BaseDir
from re_common.baselibrary.utils.basefile import BaseFile
from re_common.facade.mysqlfacade import MysqlUtiles
from re_common.baselibrary import IniConfig
from re_common.facade.loggerfacade import get_streamlogger


class Record_Count(object):
    def __init__(self):
        self.curPath = BaseDir.get_file_dir_absolute(__file__)
        self.ini_message = IniConfig(BaseFile.get_new_path(self.curPath, "config.ini")).get_conf_dict(keep_keys_case=False)
        self.mysqls = MysqlUtiles(BaseFile.get_new_path(self.curPath, "config.ini"), "db", get_streamlogger())
        self.mod = self.ini_message["mod"]["mod"]

    def judge_mod(self):
        if self.mod == "1":
            year = self.ini_message["mod_1_argument"]["year"]
            self.one_year(year)
        elif self.mod == "2":
            begin_year = self.ini_message["mod_2_argument"]["begin_year"]
            end_year = self.ini_message["mod_2_argument"]["end_year"]
            self.many_year(begin_year, end_year)
        elif self.mod == "3":
            year = self.ini_message["mod_3_argument"]["year"]
            if "," in year:
                self.jump_year(year)
        elif self.mod == "4":
            year = self.ini_message["mod_4_argument"]["year"]
            month = self.ini_message["mod_4_argument"]["month"]
            if "," in year:
                self.year_month(year,month)
        elif self.mod == "5":
            self.all_weipu()
        else:
            print("mod 参数错误")

    def one_year(self, year):
        # 题录增量
        sql = "SELECT sum(increment_metadata) FROM updating WHERE YEAR(update_date) = %s" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        now_year_add_tilu = rows[0][0]
        # 全文增量
        sql = "SELECT sum(increment_fulltext) FROM updating WHERE YEAR(update_date) = %s" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        now_year_add_quanwen = rows[0][0]
        # 引文增量
        sql = "SELECT sum(increment_ref) FROM updating WHERE YEAR(update_date) = %s" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        now_year_add_yinwen = rows[0][0]
        # 封面增量
        sql = "SELECT sum(increment_cover) FROM updating WHERE YEAR(update_date) = %s" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        now_year_add_cover = rows[0][0]

        # 题录总量
        count_metadata = 0
        sql = "SELECT max(count_metadata) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        for row in rows:
            count_metadata += int(row[0])
        # 引文总量
        count_ref = 0
        sql = "SELECT max(count_ref) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        for row in rows:
            count_ref += int(row[0])
        # 全文总量
        count_fulltext = 0
        sql = "SELECT max(count_fulltext) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        for row in rows:
            count_fulltext += int(row[0])
        # 封面总量
        count_cover = 0
        sql = "SELECT max(count_cover) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
        bool, rows = self.mysqls.SelectFromDB(sql)
        for row in rows:
            count_cover += int(row[0])

        line = "年份：" + str(year) + "\n"
        line += "题录增量：" + str(now_year_add_tilu) + "\n"
        line += "引文增量：" + str(now_year_add_yinwen) + "\n"
        line += "全文增量：" + str(now_year_add_quanwen) + "\n"
        line += "封面增量：" + str(now_year_add_cover) + "\n"
        line += "题录总量：" + str(count_metadata) + "\n"
        line += "引文总量：" + str(count_ref) + "\n"
        line += "全文总量：" + str(count_fulltext) + "\n"
        line += "封面总量：" + str(count_cover) + "\n"
        line += "************************" + "\n"
        file_path = self.curPath + "/" + str(year) + ".txt"
        with open(file_path, 'w', encoding='utf-8')as f:
            f.write(line)
        print("%s年统计结束" % year)

    def jump_year(self, year):
        year_list = year.split(",")
        file_path = self.curPath + "/" + ",".join(year_list) + ".txt"
        line = ""
        tilu_zhi = 0
        quanwen_zhi = 0
        yinwei_zhi = 0
        cover_zhi = 0
        for year in year_list:
            # 题录增量
            sql = "SELECT sum(increment_metadata) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_tilu = rows[0][0]
            tilu_zhi += int(now_year_add_tilu)
            # 全文增量
            sql = "SELECT sum(increment_fulltext) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_quanwen = rows[0][0]
            quanwen_zhi += int(now_year_add_quanwen)
            # 引文增量
            sql = "SELECT sum(increment_ref) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_yinwen = rows[0][0]
            yinwei_zhi += int(now_year_add_yinwen)
            # 封面增量
            sql = "SELECT sum(increment_cover) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_cover = rows[0][0]
            cover_zhi += int(now_year_add_cover)

            # 题录总量
            count_metadata = 0
            sql = "SELECT max(count_metadata) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_metadata += int(row[0])
            # 引文总量
            count_ref = 0
            sql = "SELECT max(count_ref) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_ref += int(row[0])
            # 全文总量
            count_fulltext = 0
            sql = "SELECT max(count_fulltext) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_fulltext += int(row[0])
            # 封面总量
            count_cover = 0
            sql = "SELECT max(count_cover) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_cover += int(row[0])

            line += "年份：" + str(year) + "\n"
            line += "题录增量：" + str(now_year_add_tilu) + "\n"
            line += "引文增量：" + str(now_year_add_yinwen) + "\n"
            line += "全文增量：" + str(now_year_add_quanwen) + "\n"
            line += "封面增量：" + str(now_year_add_cover) + "\n"
            line += "题录总量：" + str(count_metadata) + "\n"
            line += "引文总量：" + str(count_ref) + "\n"
            line += "全文总量：" + str(count_fulltext) + "\n"
            line += "封面总量：" + str(count_cover) + "\n"
            line += "************************" + "\n"
            print("%s年统计结束" % year)

        line += "年(包含),".join(year_list) + "年(包含)共:" + "\n"
        line += "题录增量：" + str(tilu_zhi) + "\n"
        line += "引文增量：" + str(yinwei_zhi) + "\n"
        line += "全文增量：" + str(quanwen_zhi) + "\n"
        line += "封面增量：" + str(cover_zhi) + "\n"
        with open(file_path, 'w', encoding='utf-8')as f:
            f.write(line)

    def many_year(self, begin_year, end_year):
        file_path = self.curPath + "/" + str(begin_year) + "——" + str(end_year) + ".txt"
        line = ""
        tilu_zhi = 0
        quanwen_zhi = 0
        yinwei_zhi = 0
        cover_zhi = 0
        for year in range(int(begin_year), int(end_year) + 1):
            # 题录增量
            sql = "SELECT sum(increment_metadata) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_tilu = rows[0][0]
            tilu_zhi += int(now_year_add_tilu)
            # 全文增量
            sql = "SELECT sum(increment_fulltext) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_quanwen = rows[0][0]
            quanwen_zhi += int(now_year_add_quanwen)
            # 引文增量
            sql = "SELECT sum(increment_ref) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_yinwen = rows[0][0]
            yinwei_zhi += int(now_year_add_yinwen)
            # 封面增量
            sql = "SELECT sum(increment_cover) FROM updating WHERE YEAR(update_date) = %s" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            now_year_add_cover = rows[0][0]
            cover_zhi += int(now_year_add_cover)

            # 题录总量
            count_metadata = 0
            sql = "SELECT max(count_metadata) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_metadata += int(row[0])
            # 引文总量
            count_ref = 0
            sql = "SELECT max(count_ref) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_ref += int(row[0])
            # 全文总量
            count_fulltext = 0
            sql = "SELECT max(count_fulltext) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_fulltext += int(row[0])
            # 封面总量
            count_cover = 0
            sql = "SELECT max(count_cover) FROM updating WHERE  YEAR(update_date) = %s  GROUP BY provider" % (year)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_cover += int(row[0])

            line += "年份：" + str(year) + "\n"
            line += "题录增量：" + str(now_year_add_tilu) + "\n"
            line += "引文增量：" + str(now_year_add_yinwen) + "\n"
            line += "全文增量：" + str(now_year_add_quanwen) + "\n"
            line += "封面增量：" + str(now_year_add_cover) + "\n"
            line += "题录总量：" + str(count_metadata) + "\n"
            line += "引文总量：" + str(count_ref) + "\n"
            line += "全文总量：" + str(count_fulltext) + "\n"
            line += "封面总量：" + str(count_cover) + "\n"
            line += "************************" + "\n"
            print("%d年统计结束" % year)
        line += "{}年(包含) —— {}年(包含):".format(str(begin_year), str(end_year)) + "\n"
        line += "题录增量：" + str(tilu_zhi) + "\n"
        line += "引文增量：" + str(yinwei_zhi) + "\n"
        line += "全文增量：" + str(quanwen_zhi) + "\n"
        line += "封面增量：" + str(cover_zhi) + "\n"
        with open(file_path, 'w', encoding='utf-8')as f:
            f.write(line)

    def year_month(self, year, month):
        dict_info = dict()
        year_list = year.split(",")
        month_list = month.split(",")
        file_path = self.curPath + "/" + ",".join(year_list) +"(" + ",".join(month_list) + ")" + ".txt"
        line = ""
        tilu_zhi = 0
        quanwen_zhi = 0
        yinwei_zhi = 0
        cover_zhi = 0
        taks__sql = "select provider from task where who like '%维普+%'and if_active = 1"
        bool, rows = self.mysqls.SelectFromDB(taks__sql)
        for info in rows:
            line = ""
            task = info[0]
            xx = '==========Provider:{}=========='.format(task)
            line += xx.center(80,' ') + '\n'
            for month in month_list:
                line += '\t月份:' + month + '\n'
                line += 8*" " + "题录增量".center(10,' ') + "引文增量".center(20,' ') + "全文增量".center(20,' ') + "封面增量".center(10,' ') + '\n'
                for year in year_list:
                    date = '{}-{}-1'.format(year,month)
                    date_n = '{}-{}-1'.format(year,int(month)+1)
                    # 题录增量
                    sql = "SELECT sum(increment_metadata) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (task,date,date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    now_year_add_tilu = rows[0][0]
                    # 全文增量
                    sql = "SELECT sum(increment_fulltext) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (task,date,date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    now_year_add_quanwen = rows[0][0]
                    # 引文增量
                    sql = "SELECT sum(increment_ref) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (task,date,date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    now_year_add_yinwen = rows[0][0]
                    # 封面增量
                    sql = "SELECT sum(increment_cover) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (task,date,date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    now_year_add_cover = rows[0][0]

                    line += str(year) + ':'
                    line += str(now_year_add_tilu).center(20,' ')
                    line += str(now_year_add_yinwen).center(20,' ')
                    line += str(now_year_add_quanwen).center(20,' ')
                    line += str(now_year_add_cover).center(20,' ') + '\n'
                    tilu = "{}_{}_{}_tilu".format(task,year,month)
                    yinwen = "{}_{}_{}_yinwen".format(task,year,month)
                    quanwen = "{}_{}_{}_quanwen".format(task, year, month)
                    cover = "{}_{}_{}_cover".format(task, year, month)
                    dict_info[tilu] = now_year_add_tilu
                    dict_info[yinwen] = now_year_add_yinwen
                    dict_info[quanwen] = now_year_add_quanwen
                    dict_info[cover] = now_year_add_cover

                tilu_2019 = "{}_{}_{}_tilu".format(task, '2019', month)
                tilu_2020 = "{}_{}_{}_tilu".format(task, '2020', month)
                old_tilu = dict_info[tilu_2019]
                if old_tilu is None:
                    old_tilu = 0
                new_tilu = dict_info[tilu_2020]
                if new_tilu is None:
                    new_tilu = 0

                yinwen_2019 = "{}_{}_{}_yinwen".format(task, '2019', month)
                yinwen_2020 = "{}_{}_{}_yinwen".format(task, '2020', month)
                old_yinwen = dict_info[yinwen_2019]
                if old_yinwen is None:
                    old_yinwen = 0
                new_yinwen = dict_info[yinwen_2020]
                if new_yinwen is None:
                    new_yinwen = 0

                quanwen_2019 = "{}_{}_{}_quanwen".format(task, '2019', month)
                quanwen_2020 = "{}_{}_{}_quanwen".format(task, '2020', month)
                old_quanwen = dict_info[quanwen_2019]
                if old_quanwen is None:
                    old_quanwen = 0
                new_quanwen = dict_info[quanwen_2020]
                if new_quanwen is None:
                    new_quanwen = 0

                cover_2019 = "{}_{}_{}_cover".format(task, '2019', month)
                cover_2020 = "{}_{}_{}_cover".format(task, '2020', month)
                old_cover = dict_info[cover_2019]
                if old_cover is None:
                    old_cover = 0
                new_cover = dict_info[cover_2020]
                if new_cover is None:
                    new_cover = 0

                line += '差:' + str(new_tilu-old_tilu).center(24, ' ') + str(new_yinwen-old_yinwen).center(15, ' ') + str(new_quanwen-old_quanwen).center(26,' ') + str(new_cover-old_cover).center(14, ' ') + '\n'
                with open(file_path, 'a', encoding='utf-8')as f:
                    f.write(line)

    def all_weipu(self):
        dict_yw = dict()
        line = ""
        tilu_all = 0
        quanwen_all = 0
        yinwen_all = 0
        taks__sql = "select provider from task where who like '%维普+%'and if_active = 1"
        bool, rows = self.mysqls.SelectFromDB(taks__sql)
        for info in rows:

            task = info[0]
            # 题录总量
            count_metadata = 0
            sql = "SELECT max(count_metadata) FROM updating WHERE  provider = '%s'" % (task)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_metadata += int(row[0])
            # 引文总量
            count_ref = 0
            sql = "SELECT max(count_ref) FROM updating WHERE  provider = '%s'" % (task)
            bool, rows = self.mysqls.SelectFromDB(sql)
            for row in rows:
                count_ref += int(row[0])


            dict_yw[task] = count_ref
            tilu_all += count_metadata
            yinwen_all += count_ref


        line += "题录总量：" + str(tilu_all) + "\n"
        line += "引文总量：" + str(yinwen_all) + "\n"
        # line += "全文总量：" + str(quanwen_all) + "\n"

        line += "************************" + "\n"
        file_path = self.curPath + "/" + 'weipu' + ".txt"
        with open(file_path, 'w', encoding='utf-8')as f:
            f.write(line)


    def all_weipu_year(self):
        dict_yw = dict()
        line = ""
        tilu_all = 0
        quanwen_all = 0
        yinwen_all = 0
        taks__sql = "select provider from task where who like '%维普+%'and if_active = 1"
        bool, rows = self.mysqls.SelectFromDB(taks__sql)
        for info in rows:

            task = info[0]
            for year in [2020]:
                # 题录总量
                count_metadata = 0
                sql = "SELECT sum(increment_metadata) FROM updating WHERE  provider = '%s' and YEAR(update_date) = '%s'" % (task,year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    try:
                        count_metadata += int(row[0])
                    except:
                        count_metadata = 0
                # 引文总量
                count_ref = 0
                sql = "SELECT sum(increment_ref) FROM updating WHERE  provider = '%s' and YEAR(update_date) = '%s'" % (task,year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    try:
                        count_ref += int(row[0])
                    except:
                        count_ref = 0


                # dict_yw[task] = count_ref
                tilu_all += count_metadata
                yinwen_all += count_ref


                line += "{}-{}-题录增加量：{}\n".format(task,year,count_metadata)
                # line += "{}-{}-引文总量：{}\n" .format(task,year,count_ref)
                # line += "全文总量：" + str(quanwen_all) + "\n"

            line += "************************" + "\n"
            file_path = self.curPath + "/" + 'weipu' + ".txt"
            with open(file_path, 'w', encoding='utf-8')as f:
                f.write(line)



if __name__ == '__main__':
    t = Record_Count()
    # t.judge_mod()
    t.all_weipu_year()